<?php

namespace common\logics\trade;

use Yii;
use common\models\trade\TradePartner;
use common\models\trade\TradePaymode;
use common\logics\trade\scope\CompatibleScopeInterface;

/**
 * 业务订单模型
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class BizOrderModel extends \common\base\Model {

    use \common\traits\Constants;
    use BizOrderModelTrait;

    /**
     * @var string 支付范围
     */
    const SCOPE_SHOP = 'shop';
    const SCOPE_YUND = 'yund';

    /**
     * @var integer 账号的UID
     */
    public $user_id;

    /**
     * @var string 业务范围名称
     */
    public $scope;

    /**
     * @var integer 业务订单ID
     */
    public $order_id;

    /**
     * @var CompatibleScopeInterface 业务订单模型
     */
    private $_model = null;

    /**
     * @var TradePartner 授权应用商户
     */
    private $_partner = null;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['user_id', 'order_id', 'scope'], 'required'],
            [['user_id', 'order_id'], 'integer'],
            ['scope', 'string', 'max' => 30],
            ['scope', 'validateScope'],
            ['order_id', 'validateOrder'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'order_id' => '订单ID',
            'user_id' => '用户ID',
            'paymode' => '支付方式',
            'scope' => '业务类型',
            'partner_id' => '商户ID',
        ];
    }

    /**
     * 校验订单信息
     */
    public function validateScope($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $partner = TradePartner::findOne(['name' => $this->$attribute]);
        if (!$partner) {
            $this->addError($attribute, '非授权的收款应用方式');
        }
        $this->_partner = $partner;
    }

    /**
     * 校验订单信息
     */
    public function validateOrder($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $model = $this->getOrderModel($this->scope, $this->order_id);
        if ($model) {
            if ($model->user_id !== $this->user_id) {
                $this->addError($attribute, '订单信息不正确，非当前用户的订单');
            } else {
                $this->setModel($model);
            }
        } else {
            $this->addError($attribute, '订单超时或者订单状态已变更');
        }
    }

    /**
     * @return CompatibleScopeInterface|null
     */
    protected function getOrderModel($type, $id) {
        if (!$this->hasMethod($type)) {
            return null;
        }
        return $this->{$type}($id);
    }

    /**
     * @return TradePartner|null
     */
    protected function getPartner() {
        return $this->_partner;
    }

    /**
     * @return TradePaymode[]
     */
    protected function getPaymodes() {
        $partner = $this->getPartner();
        if (!$partner) {
            return [];
        }
        return $partner->paymodes;
    }

    /**
     * @return CompatibleScopeInterface|null
     */
    protected function setModel($model) {
        $this->_model = $model;
    }

    /**
     * @return CompatibleScopeInterface|null
     */
    protected function getModel() {
        return $this->_model;
    }

}
